package function;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

import function.model.User;

public class RemoveDuplicates {

	static void java8List(List<String> list) {
		List<String> result = list.stream().distinct().collect(Collectors.toList());
		printList(result, "java8 remove duplicates list");
	}

	static void hasSetList(List<String> list) {
		List<String> result = new ArrayList<>();
		result.addAll(new HashSet<>(list));
		printList(result, "HasSet remove duplicates list");
	}

	static void java8Listobject(List<User> objectList) {
		List<User> result = objectList.stream().collect(Collectors.collectingAndThen(
				Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getName))), ArrayList::new));
		printListObject(result, "java8 remove duplicates list Object");
	}

	static void printList(List<String> list, String name) {
		System.out.println("********** " + name + " **********");
		for (String item : list) {
			System.out.println(item);
		}
	}

	static void printListObject(List<User> list, String name) {
		System.out.println("********** " + name + " **********");
		for (User item : list) {
			System.out.println(item);
		}
	}

	public static void main(String[] args) {

		ArrayList<String> list = new ArrayList<>(Arrays.asList("富贵", "狗蛋", "富贵", "如花", "狗蛋"));
		java8List(list);
		hasSetList(list);

		ArrayList<User> listObject = new ArrayList<>();
		listObject.add(new User("A", "富贵", 10));
		listObject.add(new User("A", "狗蛋", 10));
		listObject.add(new User("A", "富贵", 10));
		listObject.add(new User("A", "如花", 10));
		listObject.add(new User("A", "狗蛋", 10));
		java8Listobject(listObject);

	}

}
